package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.entity.CitySysOrganization;
import com.gxa.entity.DistrictSysUser;
import com.gxa.mapper.CitySysOrganizationMapper;
import com.gxa.mapper.DistrictUserMapper;
import com.gxa.service.CitySysOrganizationService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CitySysOrganizationServiceImpl implements CitySysOrganizationService {

    @Autowired
    private CitySysOrganizationMapper citySysOrganizationMapper;

    @Autowired
    private DistrictUserMapper districtUserMapper;

    //加密算法
    private final static String hashAlgorithmName = "md5";

    //散列次数
    private final static Integer hashIterations = 3;


    /**
     * 分页查询所有机构列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findOrganizations(Integer currentPage, Integer pageSize) {

        R r = new R();

        Page page =  PageHelper.startPage(currentPage,pageSize);

        List<CitySysOrganization> citySysOrganizationList = citySysOrganizationMapper.findCitySysOrganizationList();

        int total =  (int)page.getTotal();

        Map map =new HashMap();

        map.put("total",total);
        map.put("citySysOrganizationList",citySysOrganizationList);


        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }


    /**
     * 根据机构名，管理员，联系方式多条件查询机构列表
     * @param currentPage
     * @param pageSize
     * @param orgName
     * @param adminName
     * @param adminPhone
     * @return
     */
    @Override
    public R findOrgnizationByConditions(Integer currentPage, Integer pageSize, String orgName, String adminName, String adminPhone) {
        R r = new R();

        Page page = PageHelper.startPage(currentPage,pageSize);

        List<CitySysOrganization> citySysOrganizationList = citySysOrganizationMapper.findCitySysOrganizationListByConditions(orgName,adminName,adminPhone);

        int total = (int) page.getTotal();

        Map map = new HashMap();

        map.put("total",total);
        map.put("citySysOrganizationList",citySysOrganizationList);


        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }


    /**
     * 根据机构编号查询机构
     * @param id
     * @return
     */
    @Override
    public R findorganizationbyid(Integer id) {
        R r = new R();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(citySysOrganizationMapper.findorganizationbyid(id));
        return r;
    }

    /**
     * 添加机构
     * @param citySysOrganization
     * @return
     */
    @Override
    public R addorganization(CitySysOrganization citySysOrganization) {
        R r = new R();

        try{
            DistrictSysUser districtSysUser = new DistrictSysUser();

            districtSysUser.setNickname(citySysOrganization.getAdminAccount());
            districtSysUser.setPassword(citySysOrganization.getAdminPassword());
            //获取加密后的密码
            SimpleHash simpleHash = new SimpleHash(hashAlgorithmName,districtSysUser.getPassword(),districtSysUser.getNickname(),hashIterations);
            String newPwd = simpleHash.toString();

            districtSysUser.setHeadPortrait("/opt/managerHead.jpg");
            districtSysUser.setUserName(citySysOrganization.getAdminName());
            districtSysUser.setPhone(citySysOrganization.getAdminPhone());
            districtSysUser.setOrganization(citySysOrganization.getOrgName());
            districtSysUser.setRole("区级管理员");

            districtSysUser.setPassword(newPwd);
            districtSysUser.setSalt(districtSysUser.getNickname());
            districtSysUser.setIsLeader("是");
            districtSysUser.setWechat("未绑定");
            districtSysUser.setCreator(citySysOrganization.getOrgCreator());
            districtSysUser.setCreatTime(new Date());
            districtSysUser.setStatus("启用中");
            districtSysUser.setArea(citySysOrganization.getOrgName().substring(0,2));


            citySysOrganization.setAdminPassword(newPwd);
            citySysOrganization.setCreateDate(new Date());
            //判断要添加的账号是否已经存在
            DistrictSysUser selectedDistrictSysUser = districtUserMapper.selectDistrictUserByNickname(districtSysUser.getNickname());
            if(selectedDistrictSysUser == null){
                //账号不存在，进行添加
                districtUserMapper.insertDistrictUser(districtSysUser);
                citySysOrganizationMapper.addorganization(citySysOrganization);
                r.setCode(200);
                r.setMsg("添加成功！");
            }else {
                r.setCode(200);
                r.setMsg("添加失败，要添加是账号已存在！");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,添加失败,请联系管理员");
        }
        return r;
    }


    /**
     * 编辑机构
     * @param citySysOrganization
     * @return
     */
    @Override
    @Transactional
    public R updateorganization(CitySysOrganization citySysOrganization) {
        R r = new R();
        int row = citySysOrganizationMapper.updateorganization(citySysOrganization);
        if (row > 0 ){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(400);
            r.setMsg("修改失败");
        }
        return r;
    }

    /**
     * 根据机构编号删除机构
     * @param orgId
     * @return
     */
    @Override
    public R deleteorganization(int orgId) {
        R r = new R();
        int row = citySysOrganizationMapper.deleteorganization(orgId);

        if (row > 0 ){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(400);
            r.setMsg("删除失败");
        }
        return r;
    }

    /**
     * 重置机构管理员密码为123456
     * @param orgId
     * @return
     */
    @Override
    public R resetpassword(int orgId) {
        R r = new R();

        CitySysOrganization citySysOrganization = citySysOrganizationMapper.findorganizationbyid(orgId);
        //获取加密后的密码
        SimpleHash simpleHash = new SimpleHash(hashAlgorithmName,"123456",citySysOrganization.getAdminAccount(),hashIterations);
        String newPwd = simpleHash.toString();


        int row = citySysOrganizationMapper.resetpassword(orgId,newPwd);
                  citySysOrganizationMapper.resetDistrictAdminPassword(citySysOrganization.getAdminAccount(),newPwd);

        if (row > 0 ){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(400);
            r.setMsg("修改失败");
        }
        return r;
    }

}
